<template>
  <div class="mod-menu">
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="showAdd()">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      border
      style="width: 100%; ">
      <el-table-column
        prop="couponId"
        header-align="center"
        align="center"
        label="ID" >
      </el-table-column>
      <el-table-column
        prop="couponSize"
        header-align="center"
        align="center"
        label="可免金额" >
      </el-table-column>
      <el-table-column
        prop="couponSum"
        header-align="center"
        align="center"
        label="需要金额">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="开始时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="结束时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showedit(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.couponId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog title="新增优惠券" width="40%" :visible="addVisible" :before-close="handleClose">
      <el-form :model="addGoodsCoupon" label-width="80px" ref="dateForm" :rules="addRules" class="demo-ruleForm">
        <el-form-item label="可免金额" prop="couponSize">
          <el-input-number v-model="addGoodsCoupon.couponSize"></el-input-number>
        </el-form-item>
        <el-form-item label="需要金额" prop="couponSum">
          <el-input-number v-model="addGoodsCoupon.couponSum"></el-input-number>
        </el-form-item>
        <el-form-item  label="活动时间" required>
          <el-col :span="11">
            <el-form-item prop="createTime">
              <el-date-picker  value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="开始时间" v-model="addGoodsCoupon.createTime" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item prop="endTime">
              <el-date-picker   value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="结束时间" v-model="addGoodsCoupon.endTime" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addSave()">立即创建</el-button>
          <el-button @click="addVisible =false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog title="修改优惠券" width="40%" :visible="editVisible" :before-close="handleClose">
      <el-form :model="editGoodsCoupon" label-width="80px" ref="editDateForm" :rules="editRules" class="demo-ruleForm">
        <el-form-item label="可免金额" prop="couponSize">
          <el-input-number v-model="editGoodsCoupon.couponSize"></el-input-number>
        </el-form-item>
        <el-form-item label="需要金额" prop="couponSum">
          <el-input-number v-model="editGoodsCoupon.couponSum"></el-input-number>
        </el-form-item>
        <el-form-item  label="活动时间" required>
          <el-col :span="11">
            <el-form-item prop="createTime">
              <el-date-picker  value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="开始时间" v-model="editGoodsCoupon.createTime" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item prop="endTime">
              <el-date-picker   value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="结束时间" v-model="editGoodsCoupon.endTime" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addSave()">立即创建</el-button>
          <el-button @click="addVisible =false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      searchItem: {},
      addVisible: false,
      picVisible: false,
      editVisible: false,
      dialogVisible: false,
      dataForm: '',
      disabled: false,
      dialogImageUrl: '',
      addGoodsCoupon: {
        couponId: '',
        createTime: '',
        endTime: '',
        couponSum: '',
        couponSize: ''
      },
      addRules: {
        createTime: [
          {required: true, message: '请选择日期', trigger: 'change'}
        ],
        endTime: [
          {required: true, message: '请选择日期', trigger: 'change'}
        ],
        couponSum: [
          {required: true, message: '请输入需求金额', trigger: 'blur'}
        ],
        couponSize: [
          {required: true, message: '请输入可减金额', trigger: 'blur'}
        ]
      },
      editGoodsCoupon: {
        couponId: '',
        createTime: '',
        endTime: '',
        couponSum: '',
        couponSize: ''
      },
      editRules: {
        createTime: [
          {required: true, message: '请选择日期', trigger: 'change'}
        ],
        endTime: [
          {required: true, message: '请选择日期', trigger: 'change'}
        ],
        couponSum: [
          {required: true, message: '请输入需求金额', trigger: 'blur'}
        ],
        couponSize: [
          {required: true, message: '请输入可减金额', trigger: 'blur'}
        ]
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    showAdd () {
      this.addVisible = true
    },
    addSave () {
      console.log(this.addGoodsCoupon)
      // this.addGoodsCoupon.createTime = this.addGoodsCoupon.createTime.getTime()
      // this.addGoodsCoupon.endTime = this.addGoodsCoupon.endTime.getTime()
      this.$refs['dateForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/goods_coupon/add'),
            method: 'post',
            data: this.$http.adornData(this.addGoodsCoupon)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                }
              })
            }
            this.getList()
            this.addVisible = false
          })
        }
      })
    },
    getList () {
      this.$http({
        url: this.$http.adornUrl('/goods_coupon/page?page=' + this.pageIndex + '&size=' + this.pageSize),
        method: 'post',
        data: this.$http.adornData(this.searchItem)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    deleteHandle (id) {
      console.log(id)
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.jobId
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/goods_coupon/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    },
    showedit (row) {
      this.editGoodsCoupon = row
      this.editVisible = true
    },
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getList()
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.addVisible = false
          this.picVisible = false
          this.editVisible = false
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped>

</style>
